package com.lu.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lu.entity.Phone;
import com.lu.entity.User;
import com.lu.phone.utils.MobileCodeWS;
import com.lu.phone.utils.MobileCodeWSSoap;
import com.lu.service.PhoneService;
import com.lu.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

public class IndexAction extends ActionSupport{
	
	UserService userService = new UserService();
	PhoneService phoneService = new PhoneService();
	
	private Long id;
	private String username;
	private String password;
	private String phoneNO;
	private List<User> userList;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	public String execute(){
		System.out.println("indexAction");
		return "success";
	}

	public String userList() throws Exception{
		List<User> list = userService.getUserList(null);
		userList = list;
		return "success";
	}
	
	public String doLogin() throws Exception{
		System.out.println("doLogin");
		System.out.println(username+"="+password);
		Map<String,Object> map = new HashMap<>();
		map.put("username", username);
		map.put("password", password);
		User user = userService.getUser(map);
		if(user==null){
			return "error";
		}
		return "success";
	}
	
	public String doRergister(){
		try {
			User user = new User();
			user.setUsername(username);
			user.setPassword(password);
			user.setPhone(phoneNO);
			user.setStatus(1);
			Integer userId = userService.save(user);
			if(phoneNO!=null||!phoneNO.equals("")){
				if(userId!=null){
					MobileCodeWS mobileCodeWS = new MobileCodeWS();
					MobileCodeWSSoap mobileCodeWSSoap= mobileCodeWS.getMobileCodeWSSoap();
					String str = mobileCodeWSSoap.getMobileCodeInfo(phoneNO, null);
					System.out.println(str);
					System.out.println(str.indexOf("��"));
					str = str.substring(str.indexOf("��")+1, str.length());
					String[] ss = str.split(" ");
					
					Phone phone = new Phone();
					for(int i=0;i<ss.length;i++){
						phone.setProvince(ss[0]);
						phone.setCity(ss[1]);
						phone.setCardType(ss[2]);
					}
					phone.setUserId(Long.valueOf(userId));
					phoneService.save(phone);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
		return "success";
	}
	
	public String doUpdate(){
		Map<String,Object> map = new HashMap<>();
		try {
			User user = new User();
			user.setId(id);
			user.setPhone(phoneNO);
			Integer userId = userService.update(user);
			if(phoneNO!=null||!phoneNO.equals("")){
				map.put("user_id", id);
				Phone phone = phoneService.getPhone(map);
				if(phone!=null){
					MobileCodeWS mobileCodeWS = new MobileCodeWS();
					MobileCodeWSSoap mobileCodeWSSoap= mobileCodeWS.getMobileCodeWSSoap();
					String str = mobileCodeWSSoap.getMobileCodeInfo(phoneNO, null);
					str = str.substring(str.indexOf("��")+1, str.length());
					String[] ss = str.split(" ");
					for(int i=0;i<ss.length;i++){
						phone.setProvince(ss[0]);
						phone.setCity(ss[1]);
						phone.setCardType(ss[2]);
					}
					phone.setId(phone.getId());
					phoneService.update(phone);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
		return "success";
	}
	
	private String doRemove(){
		try {
			userService.remove(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
		return "success";
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPhoneNO() {
		return phoneNO;
	}

	public void setPhoneNO(String phoneNO) {
		this.phoneNO = phoneNO;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

}
